// favorites.js
Page({
  data: {
    favorites: [],
    currentShareIndex: undefined
  },

  onLoad() {
    this.loadFavorites()
  },

  onShow() {
    this.loadFavorites()
  },

  loadFavorites() {
    const favorites = wx.getStorageSync('favorites') || []
    this.setData({ favorites })
  },

  removeFavorite(e) {
    const { index } = e.currentTarget.dataset
    const favorites = [...this.data.favorites]
    favorites.splice(index, 1)
    
    wx.setStorageSync('favorites', favorites)
    this.setData({ favorites })
    
    wx.showToast({
      title: '已取消收藏',
      icon: 'success'
    })
  },

  shareName(e) {
    const index = e.currentTarget.dataset.index;
    const favorite = this.data.favorites[index];
    
    if (!favorite) {
      wx.showToast({
        title: '分享信息不完整',
        icon: 'none'
      });
      return;
    }
    
    // 保存当前要分享的收藏项索引
    this.setData({
      currentShareIndex: index
    });
    
    // 显示分享菜单
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline'],
      success: () => {
        console.log('显示分享菜单成功');
      },
      fail: (err) => {
        console.error('显示分享菜单失败:', err);
        wx.showToast({
          title: '分享功能暂不可用',
          icon: 'none'
        });
      }
    });
    
    // 显示提示
    wx.showToast({
      title: '请选择分享方式',
      icon: 'none',
      duration: 1500
    });
  },

  generateNewNames() {
    wx.navigateTo({
      url: '/pages/index/index'
    })
  },

  onShareAppMessage(res) {
    if (res.from === 'button') {
      const index = this.data.currentShareIndex;
      const favorite = this.data.favorites[index];
      
      if (favorite) {
        // 根据收藏项的方法类型生成不同的分享文案
        let shareTitle = '';
        
        switch (favorite.methodType) {
          case 'bazi':
            shareTitle = `我收藏的八字五行好名字：${favorite.fullName}，五行平衡，寓意深远！`;
            break;
          case 'culture':
            shareTitle = `我收藏的文化典故好名字：${favorite.fullName}，文化底蕴深厚！`;
            break;
          case 'combined':
            shareTitle = `我收藏的五行典故结合好名字：${favorite.fullName}，命理文化双重加持！`;
            break;
          case 'zodiac':
            shareTitle = `我收藏的生肖取名好名字：${favorite.fullName}，吉祥如意！`;
            break;
          default:
            shareTitle = `推荐一个好名字：${favorite.fullName}，寓意美好！`;
        }
        
        return {
          title: shareTitle,
          path: '/pages/index/index'
        };
      }
    }
    
    return {
      title: '新生宝宝取名精灵 - 智能起名应用',
      path: '/pages/index/index'
    };
  },
  
  /**
   * 用户点击右上角分享到朋友圈
   */
  onShareTimeline() {
    // 如果有当前选择的收藏项，使用它来生成分享内容
    if (this.data.currentShareIndex !== undefined) {
      const favorite = this.data.favorites[this.data.currentShareIndex];
      
      if (favorite) {
        // 根据收藏项的方法类型生成不同的分享文案
        let shareTitle = '';
        
        switch (favorite.methodType) {
          case 'bazi':
            shareTitle = `八字五行好名字：${favorite.fullName}，五行平衡，寓意深远！`;
            break;
          case 'culture':
            shareTitle = `文化典故好名字：${favorite.fullName}，文化底蕴深厚！`;
            break;
          case 'combined':
            shareTitle = `五行典故结合好名字：${favorite.fullName}，命理文化双重加持！`;
            break;
          case 'zodiac':
            shareTitle = `生肖取名好名字：${favorite.fullName}，吉祥如意！`;
            break;
          default:
            shareTitle = `推荐一个好名字：${favorite.fullName}，寓意美好！`;
        }
        
        return {
          title: shareTitle,
          query: ''
        };
      }
    }
    
    // 默认分享内容
    return {
      title: '新生宝宝取名精灵 - 我收藏的好名字',
      query: ''
    };
  },
  
  // 当收藏列表为空时，提供跳转到首页的方法
  goToGenerate() {
    wx.navigateTo({
      url: '/pages/index/index'
    });
  }
}) 